const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const now = new Date();
    
    // 用户表格配置示例
    const userTableConfig = {
      id: uuidv4(),
      tenant_id: null, // 系统级配置
      code: 'users',
      name: '用户管理',
      description: '系统用户信息管理表格',
      table_name: 'users',
      api_path: '/api/users',
      fields: JSON.stringify([
        {
          key: 'id',
          label: 'ID',
          type: 'text',
          table: {
            width: 200,
            align: 'center',
            fixed: 'left',
            sortable: true,
            hidden: false,
          },
          search: {
            searchable: false,
          },
          form: {
            editable: false,
            creatable: false,
            updatable: false,
          },
        },
        {
          key: 'email',
          label: '邮箱',
          type: 'text',
          table: {
            width: 200,
            sortable: true,
          },
          search: {
            searchable: true,
            searchType: 'like',
            quickSearch: true,
          },
          form: {
            editable: true,
            creatable: true,
            updatable: true,
            rules: [
              {
                required: true,
                message: '请输入邮箱',
              },
              {
                pattern: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$',
                message: '请输入有效的邮箱地址',
              },
            ],
            placeholder: '请输入邮箱地址',
          },
        },
        {
          key: 'firstName',
          label: '名字',
          type: 'text',
          table: {
            width: 120,
          },
          search: {
            searchable: true,
            searchType: 'like',
          },
          form: {
            editable: true,
            creatable: true,
            updatable: true,
            placeholder: '请输入名字',
          },
        },
        {
          key: 'lastName',
          label: '姓氏',
          type: 'text',
          table: {
            width: 120,
          },
          search: {
            searchable: true,
            searchType: 'like',
          },
          form: {
            editable: true,
            creatable: true,
            updatable: true,
            placeholder: '请输入姓氏',
          },
        },
        {
          key: 'type',
          label: '用户类型',
          type: 'select',
          table: {
            width: 120,
          },
          search: {
            searchable: true,
            searchType: 'equal',
          },
          form: {
            editable: true,
            creatable: true,
            updatable: true,
            options: [
              { label: '超级管理员', value: 'SUPER_ADMIN' },
              { label: '租户管理员', value: 'TENANT_ADMIN' },
              { label: '普通用户', value: 'USER' },
            ],
          },
        },
        {
          key: 'status',
          label: '状态',
          type: 'select',
          table: {
            width: 100,
          },
          search: {
            searchable: true,
            searchType: 'equal',
          },
          form: {
            editable: true,
            creatable: true,
            updatable: true,
            options: [
              { label: '激活', value: 'ACTIVE' },
              { label: '未激活', value: 'INACTIVE' },
              { label: '已禁用', value: 'DISABLED' },
            ],
          },
        },
        {
          key: 'createdAt',
          label: '创建时间',
          type: 'datetime',
          table: {
            width: 180,
            sortable: true,
          },
          search: {
            searchable: true,
            searchType: 'range',
          },
          form: {
            editable: false,
            creatable: false,
            updatable: false,
          },
        },
        {
          key: 'updatedAt',
          label: '更新时间',
          type: 'datetime',
          table: {
            width: 180,
            sortable: true,
          },
          search: {
            searchable: false,
          },
          form: {
            editable: false,
            creatable: false,
            updatable: false,
          },
        },
      ]),
      actions: JSON.stringify({
        create: true,
        update: true,
        delete: true,
        batchDelete: true,
        export: true,
        import: false,
      }),
      table_props: JSON.stringify({
        bordered: false,
        size: 'middle',
        pagination: {
          pageSize: 10,
          pageSizeOptions: [10, 20, 50, 100],
          showSizeChanger: true,
          showQuickJumper: true,
        },
        rowKey: 'id',
        rowSelection: true,
      }),
      permissions: JSON.stringify({
        view: 'user:read',
        create: 'user:create',
        update: 'user:update',
        delete: 'user:delete',
      }),
      status: true,
      created_at: now,
      updated_at: now,
    };

    // 角色表格配置
    const roleTableConfig = {
      id: uuidv4(),
      tenant_id: null,
      code: 'roles',
      name: '角色管理',
      description: '系统角色管理表格',
      table_name: 'roles',
      api_path: '/api/roles',
      fields: JSON.stringify([
        {
          key: 'id',
          label: 'ID',
          type: 'text',
          table: {
            width: 200,
            align: 'center',
            fixed: 'left',
            hidden: true,
          },
          search: {
            searchable: false,
          },
          form: {
            editable: false,
            creatable: false,
            updatable: false,
          },
        },
        {
          key: 'name',
          label: '角色名称',
          type: 'text',
          table: {
            width: 150,
            sortable: true,
          },
          search: {
            searchable: true,
            searchType: 'like',
            quickSearch: true,
          },
          form: {
            editable: true,
            creatable: true,
            updatable: true,
            rules: [
              {
                required: true,
                message: '请输入角色名称',
              },
            ],
            placeholder: '请输入角色名称',
          },
        },
        {
          key: 'code',
          label: '角色编码',
          type: 'text',
          table: {
            width: 150,
            sortable: true,
          },
          search: {
            searchable: true,
            searchType: 'equal',
          },
          form: {
            editable: true,
            creatable: true,
            updatable: false,
            rules: [
              {
                required: true,
                message: '请输入角色编码',
              },
            ],
            placeholder: '请输入角色编码（英文）',
          },
        },
        {
          key: 'description',
          label: '描述',
          type: 'textarea',
          table: {
            width: 250,
            ellipsis: true,
          },
          search: {
            searchable: false,
          },
          form: {
            editable: true,
            creatable: true,
            updatable: true,
            placeholder: '请输入角色描述',
          },
        },
        {
          key: 'status',
          label: '状态',
          type: 'switch',
          table: {
            width: 100,
            align: 'center',
          },
          search: {
            searchable: true,
            searchType: 'equal',
          },
          form: {
            editable: true,
            creatable: true,
            updatable: true,
          },
        },
        {
          key: 'createdAt',
          label: '创建时间',
          type: 'datetime',
          table: {
            width: 180,
            sortable: true,
          },
          search: {
            searchable: false,
          },
          form: {
            editable: false,
            creatable: false,
            updatable: false,
          },
        },
      ]),
      actions: JSON.stringify({
        create: true,
        update: true,
        delete: true,
        batchDelete: false,
        export: true,
        import: false,
      }),
      table_props: JSON.stringify({
        bordered: false,
        size: 'middle',
        pagination: {
          pageSize: 10,
        },
        rowKey: 'id',
      }),
      permissions: JSON.stringify({
        view: 'role:read',
        create: 'role:create',
        update: 'role:update',
        delete: 'role:delete',
      }),
      status: true,
      created_at: now,
      updated_at: now,
    };

    // 插入数据
    await queryInterface.bulkInsert('table_configs', [
      userTableConfig,
      roleTableConfig,
    ]);
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('table_configs', {
      code: { [Sequelize.Op.in]: ['users', 'roles'] },
    });
  },
};
